package com.baizhi;

import com.baizhi.dao.CmfzGuruDao;
import com.baizhi.dao.CmfzUserDao;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.entity.CmfzUser;
import com.baizhi.util.GenerateUserUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Generated;
import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes =CmfzdemoApplication.class )
public class testPOI2 {

    @Resource
    private CmfzGuruDao cmfzGuruDao;

    @Test
    public void test1() throws IOException {

        //模拟从数据库查到的所有用户及用户信息
        List<CmfzGuru> list = cmfzGuruDao.selectList(null);
        for (CmfzGuru cmfzGuru : list) {
            System.out.println(cmfzGuru);
        }
        //创建工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("guru");
        //创建标题栏
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        String[] titles = new String[]{"上师编号","上师图片地址", "上师姓名", "上师法名", "上师状态"};
        for (int i = 0; i < titles.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(titles[i]);

        }
        for (int i = 1; i <= list.size(); i++) {
            //创建数据行对象
            row = sheet.createRow(i);
            //数据行第一列设值
            cell = row.createCell(0);
            cell.setCellValue(list.get(i - 1).getGuruId());
            //数据行第二列设值
            cell = row.createCell(1);
            cell.setCellValue(list.get(i - 1).getGuruImage());
            //数据行第三列设值
            cell = row.createCell(2);
            cell.setCellValue(list.get(i - 1).getGuruName());

            cell = row.createCell(3);
            cell.setCellValue(list.get(i - 1).getGuruNickname());

            cell = row.createCell(4);
            cell.setCellValue(list.get(i - 1).getGuruStatus());

        }
        workbook.write(new FileOutputStream("F://框架阶段//上师数据.xls"));
        workbook.close();
    }

    @Autowired
    private CmfzUserDao cmfzUserDao;

    @Test
    public void test2(){
        System.out.println(GenerateUserUtil.getUser());
        for (int i = 0; i < 100; i++) {
            ArrayList<CmfzUser> cmfzUsers = new ArrayList<>();
            for (int j = 0; j <1000; j++) {
                cmfzUsers.add(GenerateUserUtil.getUser());
            }
            try {
                cmfzUserDao.add(cmfzUsers);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



}